/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package md.sv.dbu;

import md.sv.dbu.config.XmlParserImpl;
import md.sv.dbu.executor.*;
import md.sv.dbu.receiver.FileMessageReceiver;
import md.sv.dbu.receiver.MQMessageReceiver;
import md.sv.dbu.sender.MockMessageSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 *
 */
public class DBU {

    private static final Logger logger = LoggerFactory.getLogger(DBU.class);

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length > 0) {
            DBUExecutor dbuExecutor = new DBUExecutor(new XmlParserImpl(), new MockMessageSender());
            try {
                dbuExecutor.init();
                dbuExecutor.addDbuTypeExecutor(new ConfigParser());
                dbuExecutor.addDbuTypeExecutor(new UpdateTaskExecutor());
                dbuExecutor.addDbuTypeExecutor(new MQReceiveExecutor(new MQMessageReceiver()));
                dbuExecutor.addDbuTypeExecutor(new FileReceiveExecutor(new FileMessageReceiver(dbuExecutor)));
                dbuExecutor.addDbuTypeExecutor(new SendTaskExecutor());
                dbuExecutor.execute(new File(args[0]), DBU.class.getResourceAsStream("configXMD.xsd"));
                dbuExecutor.close();
            } catch (Exception e) {
                logger.error("Application error: ", e);
            }
        } else {
            logger.info("call procedure: <program_name> [config_file_name] [termination_timeout]");
            logger.warn("No config file found. Exiting...");
        }

        // sleep before terminating
        try {
            int time = 30;
            if (args.length > 1) {
                try {
                    time = Integer.parseInt(args[1]);
                    time = time * 1000;
                } catch (Exception e) {
                    time = 30;
                }
            }

            logger.info("program will terminate in {} seconds", time / 1000);

            //do what you want to do before sleeping
            Thread.sleep(time);
            //do what you want to do after sleeping
        } catch (InterruptedException ie) {
            logger.warn("Thread wait was interrupted", ie);
        }

    }
}
